//
//  PurchaseContactTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/8/30.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PurchaseContactTableViewController: RefreshedTableViewController {

    let requestDatePicker = UIDatePicker(frame: CGRect.init(origin: CGPoint.zero, size: CGSize(width: UIScreen.main.bounds.size.width, height: 220)))
    
    let field = UITextField()
    
    override func viewDidLoad() {
        self.cansearch = true
        super.viewDidLoad()
        self.tableView.register(UINib.init(nibName: "PurchaseContactCell", bundle: Bundle.main), forCellReuseIdentifier: "PurchaseContactCell")
        self.view.addSubview(field)
        requestDatePicker.locale = Locale.init(identifier: "zh_CN")
        requestDatePicker.backgroundColor = UIColor.white
        requestDatePicker.datePickerMode = .date
        field.inputView = self.requestDatePicker
        field.inputAccessoryView = self.toolbarview
        self.searchHeader.endNotNeeded = true
    }
    
    @objc func chooseDate()->Void{
        self.field.becomeFirstResponder()
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        let startTime = searchBar.text ?? ""
        let orgC = orgCode
        if name == "refresh" {
            var dic: [String: String] = [:]
            if startTime.count > 0 {
                dic["startTime"] = startTime
            }
            if orgC.count > 0 {
                dic["orgCode"] = orgC
            }
            return ["curPage":String(page),"pageSize":"20"] + dic
        } else if name == "next" {
            var dic: [String: String] = [:]
            if startTime.count > 0 {
                dic["startTime"] = startTime
            }
            if orgC.count > 0 {
                dic["orgCode"] = orgC
            }
            return ["curPage":String(page + 1),"pageSize":"20"] + dic
        } else {
            return ["":""]
        }
    }
    @objc override func clear()->Void{
        var owncompany = JSON()
        for item in companys {
            if item.contents?["code"]?.rawString() == User.orgCode {
                owncompany = item
                break
            }
        }
        self.orgField.text = owncompany.contents?["name"]?.rawString()
        self.orgCode = User.orgCode
        self.searchBar.text = ""
        self.beginRefresh()
    }
    
    override func endedit() {
        let header = self.searchHeader
        if let field = header.firstresponder {
            if let picker = header.currentpicker as? UIDatePicker {
                field.text = picker.date.dateLineStr
            }
        }
        self.searchHeader.endedit()
        self.view.endEditing(true)
        let begindate = self.datePicker.date
        let enddate = self.endPicker.date
        let interval = begindate.timeIntervalSince(enddate)
        if interval > 0 {
            self.searchBar.text = ""
            self.beginRefresh()
        }
        self.beginRefresh()
    }
    
    
    
    
    override func canceledit() {
        super.canceledit()
    }
    
    var date: String {
        get{
            return self.requestDatePicker.date.dateLineStr
        }
    }
    
    override func path(for name: String) -> String {
        return PurchaseContactOrder
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "PurchaseContactCell")!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 65
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if let cel = cell as? PurchaseContactCell {
            cel.model = self.source[indexPath.row]
        }
        
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        let json = self.source[indexPath.row]
        
        if let id = json.contents?["id"]?.rawString() {
            
            let detail = PurchaseContactDetailViewController()
            
            detail.title = "采购项详情"
            
            detail.applyid = id
            
            self.push(destination: detail)
            
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    

}
